package com.ruoyi.project.monitor.job.wxprogram;

import com.ruoyi.project.wechatprogram.ordermaster.domain.WxOrderMaster;
import com.ruoyi.project.wechatprogram.ordermaster.service.IWxOrderMasterService;
import com.ruoyi.project.wechatprogram.wxtimerlog.domain.WxTimerLog;
import com.ruoyi.project.wechatprogram.wxtimerlog.service.IWxTimerLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 订单超时定时任务
 *
 * @author xinchen
 */
@Component("orderTimer")
public class OrderTimer
{
    private static final Logger log = LoggerFactory.getLogger(OrderTimer.class);

    @Autowired(required = false)
    private IWxOrderMasterService wxOrderMasterService;
    @Autowired(required = false)
    private IWxTimerLogService wxTimerLogService;

    public void OrderTimer(){
        try{
            Long startTime = System.currentTimeMillis();
            WxTimerLog wxTimerLog = new WxTimerLog();
            wxTimerLog.setStartTime(new Date());
            wxTimerLog.setName("订单超时定时器");
            wxTimerLog.setTaskDescribe("订单超时改变订单状态为已过期的状态");
            //----------------定时任务开始-------------------------
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date =new Date();

            WxOrderMaster wxOrderMaster = new WxOrderMaster();
            wxOrderMaster.setDay(dateFormat.format(date));
            wxOrderMaster.setOrderStatus(0);
            wxOrderMaster.setPayStatus(0);
            List<WxOrderMaster> list = wxOrderMasterService.selectWxOrderMasterList(wxOrderMaster);
            for (WxOrderMaster order : list) {
                order.setOrderStatus(3);
                order.setUpdateTime(new Date());
                wxOrderMasterService.updateOrderStatusByDay(order);
            }

            //----------------定时任务结束-------------------------
            wxTimerLog.setEndTime(new Date());
            Long endTime = System.currentTimeMillis();
            Long time = endTime-startTime;
            wxTimerLog.setElapsedTime(String.valueOf(time));
            wxTimerLogService.insertWxTimerLog(wxTimerLog);

        }catch (Exception e){
            WxTimerLog wxTimerLog = new WxTimerLog();
            wxTimerLog.setName("订单超时定时器");
            wxTimerLog.setTaskDescribe("订单超时定时器任务异常！");
            wxTimerLogService.insertWxTimerLog(wxTimerLog);
            log.error("订单超时定时任务异常！",e);
        }

    }




//    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
//    {
//        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
//    }
//
//    public void ryParams(String params)
//    {
//        System.out.println("执行有参方法：" + params);
//    }
//
//    public void ryNoParams()
//    {
//        System.out.println("执行无参方法");
//    }
}
